package tk.mynoteapp.dev;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;

public class AES256EncryptionUtil {
    public static final String ALGORITHM = "AES/ECB/PKCS7Padding";

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    /**
     * 根据密钥生成加密key(字节数组形式)
     * @param password
     * @return
     */
    private static byte[] getKeyByte(String password) {
        byte[] seed = new byte[24];
        if(password != null) {
            seed = password.getBytes();
        }
        return seed;
    }

    /**
     * 加密
     * @param data 待加密数据
     * @param password 密钥
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String password) throws Exception{
        String content = "";
        byte[] keyByte = getKeyByte(password);
        // 生成加密解密需要的Key
        SecretKeySpec keySpec = new SecretKeySpec(keyByte,"AES");
        byte[] byteContent = data.getBytes("utf-8");
        Cipher cipher = Cipher.getInstance(ALGORITHM, "BC");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] result = cipher.doFinal(byteContent);
        content = bytesToHexStr(result);
        return content;
    }

    /**
     * 解密
     * @param data 待解密数据
     * @param password 密钥
     * @return
     * @throws Exception
     */
    public static String decrypt(String data, String password) throws Exception{
        String content = "";
        byte[] keyByte = getKeyByte(password);
        byte[] byteContent = hexStrToBytes(data);
        Cipher cipher = Cipher.getInstance(ALGORITHM, "BC");
        SecretKeySpec keySpec = new SecretKeySpec(keyByte,"AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        byte[] decoded = cipher.doFinal(byteContent);
        content = new String(decoded);
        return content;
    }

    /**
     * 字节数组转十六进制形式字符串
     * @param buf 字节数组
     * @return
     */
    private static String bytesToHexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 十六进制形式字符串转字节数组
     * @param hexStr
     * @return
     */
    private static byte[] hexStrToBytes(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        }
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        String text = "hello world";
        String crypt = encrypt(text, "1234567891111111");
        System.out.println(crypt); // 27145B8CDF8DF7AD02A093A4FE0A928A
        String t = decrypt(crypt, "1234567891111111");
        System.out.println(t); // hello world
    }
}
